<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/31
 * Time: 下午10:04
 */

namespace Home\Model;
use Think\Model;

class CollectModel extends Model {

    protected $tableName = 'collect';
    protected $pk        = 'id';


    //获取收藏的用户信息
    public function getCollectUser($user_id){
        $collect_field = array('collected_id');
        $collect_order = '`collect_time` desc';
        $collect_map['collecter_id'] = $user_id;
        $collect_map['collect_type'] = 1;
        $collect_result = $this->field($collect_field)->order($collect_order)->where($collect_map)->select();
        if(is_array($collect_result)){
            foreach($collect_result as &$collect_result_one){
                //获取user表信息
                $user_field = array('user_name','image_url');
                $user_map['user_id'] = $collect_result_one['collected_id'];
                $user_db = M('User');
                $user_result = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_result)){
                    $collect_result_one['user_name'] = $user_result[0]['user_name'];
                    $collect_result_one['image_url'] = $user_result[0]['image_url'];
                }else{
                    //user表获取信息失败
                }

                //获取user_ext表信息
                $user_ext_field = array('trust','level_j','level_y');
                $user_ext_map['user_id'] = $collect_result_one['collected_id'];
                $user_ext_db = M('User_ext');
                $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                if(is_array($user_ext_result)){
                    $collect_result_one['trust'] = $user_ext_result[0]['trust'];
                    $collect_result_one['level_j'] = $user_ext_result[0]['level_j'];
                    $collect_result_one['level_y'] = $user_ext_result[0]['level_y'];
                }else{
                    //user_ext表获取信息失败
                }
            }
            return $collect_result;
        }else{
            return false;
        }
    }

    //获取收藏的任务信息
    public function getCollectMission($user_id){
        $collect_field = array('collected_id');
        $collect_order = '`collect_time` desc';
        $collect_map['collecter_id'] = $user_id;
        $collect_map['collect_type'] = 2;
        $collect_result = $this->field($collect_field)->order($collect_order)->where($collect_map)->select();
        //dump($collect_result);
        if(is_array($collect_result)){
            foreach($collect_result as &$collect_result_one){
                //获取mission表信息
                $mission_field = array('mission_name','jobs','duration','salary1','salary2','location1','location2','publisher_name','agent','publish_time');
                $mission_order = '`publish_time` desc';
                $mission_map['mission_id'] = array('eq',$collect_result_one['collected_id']);
                $mission_db = M('Mission');
                $mission_result = $mission_db->field($mission_field)->order($mission_order)->where($mission_map)->select();
                //dump($mission_result);
                if(is_array($mission_result)){
                    $collect_result_one['mission_name'] = $mission_result[0]['mission_name'];
                    $collect_result_one['jobs'] = $mission_result[0]['jobs'];
                    $collect_result_one['duration'] = $mission_result[0]['duration'];
                    $collect_result_one['salary1'] = $mission_result[0]['salary1'];
                    $collect_result_one['salary2'] = $mission_result[0]['salary2'];
                    $collect_result_one['location1'] = $mission_result[0]['location1'];
                    $collect_result_one['location2'] = $mission_result[0]['location2'];
                    $collect_result_one['publisher_name'] = $mission_result[0]['publisher_name'];
                    $collect_result_one['agent'] = $mission_result[0]['agent'];
                    $collect_result_one['publish_time'] = $mission_result[0]['publish_time'];
                }else{
                    //mission表获取信息失败
                }

                //获取user_ext表信息
                $user_ext_field = array('trust','level_j');
                $user_ext_map['user_id'] = array('eq',$collect_result_one['collected_id']);
                $user_ext_db = D('UserExt');
                $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_result);
                if(is_array($user_ext_result)){
                    $collect_result_one['trust'] = $user_ext_result[0]['trust'];
                    $collect_result_one['level_j'] = $user_ext_result[0]['level_j'];
                }else{
                    //user_ext表获取信息失败
                }
            }
            return $collect_result;
        }else{
            return false;
        }
    }

}